local jilei = fk.CreateSkill {

  name = "joy__jilei",

  tags = {  },

}



jilei:addEffect(fk.Damaged, {
  name = "joy__jilei",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jilei.name) and data.from and not data.from.dead
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, jilei.name, nil, "#joy__jilei-invoke::"..data.from.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askForChoice(player, {"basic", "trick", "equip"}, jilei.name)
    local cards
    if choice == "basic" then
      cards = room:getCardsFromPileByRule(".|.|.|.|.|basic")
    elseif choice == "trick" then
      cards = room:getCardsFromPileByRule(".|.|.|.|.|trick")
    elseif choice == "equip" then
      cards = room:getCardsFromPileByRule(".|.|.|.|.|equip")
    end
      if #cards > 0 and not player.dead then
        local get = cards[1]
        room:obtainCard(player, get, false, fk.ReasonDraw)
      end
    local mark = data.from:getMark("@joy__jilei")
    if mark == 0 then mark = {} end
    table.insertIfNeed(mark, choice .. "_char")
    room:setPlayerMark(data.from, "@joy__jilei", mark)
  end,
})
jilei:addEffect(fk.TurnStart, {
  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@joy__jilei") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@joy__jilei", 0)
  end,
})

jilei:addEffect("prohibit", {
  name = "#joy__jilei_prohibit",
  prohibit_use = function(self, player, card)
    local mark = player:getMark("@joy__jilei")
    if type(mark) == "table" and table.contains(mark, card:getTypeString() .. "_char") then
      local subcards = card:isVirtual() and card.subcards or {card.id}
      return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
  prohibit_response = function(self, player, card)
    local mark = player:getMark("@joy__jilei")
    if type(mark) == "table" and table.contains(mark, card:getTypeString() .. "_char") then
      local subcards = card:isVirtual() and card.subcards or {card.id}
      return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
  prohibit_discard = function(self, player, card)
    local mark = player:getMark("@joy__jilei")
    if type(mark) == "table" and table.contains(mark, card:getTypeString() .. "_char") then
      local subcards = card:isVirtual() and card.subcards or {card.id}
      return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
})

return jilei